<!-- 统计真实表名数量 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="站点+小组" prop="name" label-width="80px" size="mini">
        <el-cascader
          v-model="cascaderId"
          :options="cascaderList"
          @change="cascaderChange"
          :props="props"
          :clearable="true"></el-cascader>
      </el-form-item>

      <el-form-item label="异常数量" prop="errorData">
        <el-radio v-for="item in statusOptions" v-model="queryParams.errorData" size="mini" :label="item.dictValue">{{item.dictLabel}}</el-radio>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['hps:group:export']"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="groupList" @selection-change="handleSelectionChange">
      <el-table-column label="站点id-小组id" align="center" :formatter="indexMethod" />
      <el-table-column label="站点名" align="center" prop="siteName" />
      <el-table-column label="组名" align="center" prop="group.name" />
      <el-table-column label="总数量" align="center" prop="group.num" />
      <el-table-column label="已报人数" align="center" prop="num" />
      <el-table-column label="真实报名数" align="center" prop="joinCount" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { joinCountSiteGroup } from "@/api/hps/group";
import {cascaderSite} from "@/api/hps/site";

export default {
  name: "enroll",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 状态字典
      statusOptions: [{
        dictLabel: "否",
        dictValue: "0"
      },{
        dictLabel: "是",
        dictValue: "1"
      }],
      // 总条数
      total: 0,
      //提交form表单确认按钮加载效果
      submitLoading:false,
      // 环鄱赛分组表格数据
      groupList: [],
      cascaderList: [],
      props: {
        value: "id",
        label: "name",
        children: "hpsSiteGroupList",
        checkStrictly: true
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        num: undefined,
        minAge: undefined,
        maxAge: undefined,
        status: undefined,
        wxClientId: undefined,
        gender:undefined,
        siteId: undefined,
        groupId: undefined,
        errorData: '0'
      },
      cascaderId: undefined,
      // 表单参数
      form: {},
      // 表单校验
      rules: {name: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
        num: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
        minAge: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
        maxAge: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
        gender: [
          { required: true, message: '请选择状态', trigger: 'blur' }
        ],
      }
    };
  },
  created() {
    cascaderSite().then(response => {
      this.cascaderList = response.data
      this.cascaderList.forEach(item => {
        item.id = item.id
        if (item.hpsSiteGroupList && item.hpsSiteGroupList.length > 0) {
          item.hpsSiteGroupList.forEach(i => {
            i.id = i.groupId;
            i.name = i.groupName
          })
        } else {
          item.hpsSiteGroupList = undefined
        }
      })
    })
    this.getList();
    // this.getDicts("system_common_status").then(response => {
    //   this.statusOptions = response.data;
    // });
  },
  methods: {
    /** 查询环鄱赛分组列表 */
    getList() {
      this.loading = true;
      joinCountSiteGroup(this.queryParams).then(response => {
        this.groupList = response.rows;
        this.total = response.total;
        this.loading = false;
      });

    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        num: undefined,
        minAge: undefined,
        maxAge: undefined,
        status: "0",
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        wxClientId: undefined,
        gender:undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.cascaderId = undefined;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.submitLoading=false;
      this.open = true;
      this.title = "添加环鄱赛分组";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGroup(id).then(response => {
        this.form = response.data;
        this.submitLoading=false;
        this.open = true;
        this.title = "修改环鄱赛分组";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            //确认按钮显示加载中，无法点击
            this.submitLoading=true;
            updateGroup(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            //确认按钮显示加载中，无法点击
            this.submitLoading=true;
            addGroup(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除环鄱赛分组编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delGroup(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有环鄱赛分组数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return exportGroup(queryParams);
      }).then(response => {
        this.download(response.msg);
      }).catch(function() {});
    },
    cascaderChange(value) {
      console.log(value);
      this.cascaderId = value;
      if (value == undefined || value.length == 0) {
        this.queryParams.siteId = undefined;
        this.queryParams.groupId = undefined;
      } else {
        if (value.length == 1) {
          this.queryParams.siteId = value[0]
        }
        if (value.length == 2) {
          this.queryParams.siteId = value[0]
          this.queryParams.groupId = value[1]
        }
      }
    },
    indexMethod(row) {
      return row.siteId + "-" + row.groupId
    }
  }
};
</script>

